import 'package:enjoy_community_flutter/pages/login/index.dart';
import 'package:enjoy_community_flutter/pages/notfound/not_found.dart';
import 'package:enjoy_community_flutter/pages/notice/index.dart';
import 'package:enjoy_community_flutter/pages/profile/index.dart';
import 'package:enjoy_community_flutter/pages/tab_bar_page.dart';
import 'package:enjoy_community_flutter/pages/house/building_list.dart';
import 'package:enjoy_community_flutter/pages/house/house_detail.dart';
import 'package:enjoy_community_flutter/pages/house/house_form.dart';
import 'package:enjoy_community_flutter/pages/house/house_list.dart';
import 'package:enjoy_community_flutter/pages/house/location_list.dart';
import 'package:enjoy_community_flutter/pages/house/room_list.dart';
import 'package:enjoy_community_flutter/utils/token.dart';
import 'package:flutter/material.dart';

class RouterManager {
  static Map<String, WidgetBuilder> routes = {
    '/': (context) => const TabBarPage(),
    '/detail': (context) => const NoticeDetail(),
    '/login': (context) => const LoginPage(),
  };

  // 登录拦截 - 执行时机： 访问的路由地址不是上方routes中的配置地址
  // 返回router对象的话就解析并加载渲染
  // 返回null 什么都不做
  static Route<dynamic> onGenerateRoute(RouteSettings settings) {
    /*  
      分析：
        不需要拦截的页面： 首页（home页和mine页）、公告详情页、登录页
        其他页面都需要检查登录状态
          1.判断访问的页面是否是登录页
            如果不是登录页，并且token不存在，拦截到登录页
          2.如果访问的时个人信息也卖弄，token存在，正常放行
          3.非以上情况 直接返回null
    */
    // 使用??如果token为空则返回''
    final token = TokenManager().getToken() ?? '';
    if (token.isEmpty && settings.name != '/login') {
      return MaterialPageRoute(builder: (context) => const LoginPage());
    }
    if (settings.name == '/profile') {
      return MaterialPageRoute(
          builder: (context) =>
              ProfilePage(userInfo: settings.arguments as Map));
    }
    if (settings.name == '/house_list') {
      return MaterialPageRoute(builder: (context) => const HouseList());
    }
    if (settings.name == '/location_list') {
      return MaterialPageRoute(builder: (context) => const LocationList());
    }
    if (settings.name == '/building_list') {
      return MaterialPageRoute(
          builder: (context) =>
              BuildingList(point: (settings.arguments as Map)['point']));
    }
    if (settings.name == '/room_list') {
      Map temp = settings.arguments as Map;
      return MaterialPageRoute(
          builder: (context) => RoomList(
                point: temp['point'],
                building: temp['building'],
              ));
    }
    if (settings.name == '/house_form') {
      return MaterialPageRoute(
          builder: (context) => HouseForm(
                params: settings.arguments as Map,
              ));
    }
    if (settings.name == '/house_detail') {
      return MaterialPageRoute(
          builder: (context) =>
              HouseDetail(id: (settings.arguments as Map)['id']));
    }
    // 非以上 - 去404页面
    return MaterialPageRoute(builder: (context) => const NotFound());
  }
}
